package com.chen.librarysystem.mapper.impl;

import com.chen.librarysystem.context.AppContext;
import com.chen.librarysystem.context.Constant;
import com.chen.librarysystem.entry.Book;
import com.chen.librarysystem.entry.User;
import com.chen.librarysystem.log.LibraryLog;
import com.chen.librarysystem.mapper.FileMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.logging.Level;

public class FileMapperImpl implements FileMapper {

    @Override
    public boolean saveBookData(Map<String, Book> bookMap) {
        try (
                BufferedWriter bfd = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Constant.FILE_PATH + Constant.BOOK_FILE_NAME), StandardCharsets.UTF_8))
        ) {
            bfd.write(AppContext.getInstance().getMapper().writeValueAsString(bookMap));
            return true;
        } catch (Exception e) {
            LibraryLog.log(Level.SEVERE, "文件写入失败！");
            throw new RuntimeException("文件写入失败！");
        }
    }

    @Override
    public Map<String, Book> loadBookData() {
        ObjectMapper mapper = AppContext.getInstance().getMapper();
        // 在这里创建文件对象，是为了判断文件是否存在或者是否为空
        File file = new File(Constant.FILE_PATH + Constant.BOOK_FILE_NAME);
        try (
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(Constant.FILE_PATH + Constant.BOOK_FILE_NAME), StandardCharsets.UTF_8))
        ) {
            if (!file.exists() || file.length() == 0) return Map.of();
            return mapper.readValue(reader, new TypeReference<>() {
            });
        } catch (Exception e) {
            LibraryLog.log(Level.SEVERE, "加载数据失败");
        }
        return Map.of();
    }

    @Override
    public boolean saveUserData(Map<String, User> userMap) {
        try (
                BufferedWriter bfd = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Constant.FILE_PATH + Constant.USER_FILE_NAME), StandardCharsets.UTF_8))
        ) {
            bfd.write(AppContext.getInstance().getMapper().writeValueAsString(userMap));
            return true;
        } catch (Exception e) {
            LibraryLog.log(Level.SEVERE, "文件写入失败！");
            throw new RuntimeException("文件写入失败！");
        }
    }

    @Override
    public Map<String, User> loadUserData() {
        ObjectMapper mapper = new ObjectMapper();
        AppContext.getInstance().setMapper(mapper);
        // 在这里创建文件对象，是为了判断文件是否存在或者是否为空
        File file = new File(Constant.FILE_PATH + Constant.USER_FILE_NAME);
        try (

                BufferedReader bfd = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))
        ) {
            if (!file.exists() || file.length() == 0) return Map.of();
            return mapper.readValue(bfd, new TypeReference<>() {
            });
        } catch (Exception e) {
            LibraryLog.log(Level.SEVERE, "文件读取失败！请检查文件是否存在！");
            throw new RuntimeException("文件读取失败！请检查文件是否存在！");
        }
    }
}
